package com.drinkian.business.record.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.drinkian.authentication.entity.po.User;
import com.drinkian.authentication.mapper.UserMapper;
import com.drinkian.business.good.entity.po.Good;
import com.drinkian.business.good.mapper.GoodMapper;
import com.drinkian.business.record.entity.po.Record;
import com.drinkian.business.record.mapper.RecordMapper;
import com.drinkian.business.record.service.IRecordService;
import com.drinkian.business.room.entity.po.Room;
import com.drinkian.business.room.mapper.RoomMapper;
import com.drinkian.entity.RequestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

@Service
public class RecordServiceImpl implements IRecordService {

    @Autowired
    private RecordMapper recordMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private GoodMapper goodMapper;
    @Autowired
    private RoomMapper roomMapper;


    @Override
    public RequestResult getRecord(Integer current, Integer size, Integer userType, Long userId, Integer itemType) {
        IPage<Record> iPage = new Page<>(current, size);
        QueryWrapper<Record> recordQueryWrapper = new QueryWrapper<>();
        if (userType == 1) {
            recordQueryWrapper.eq("seller_id", userId);
            recordQueryWrapper.eq("seller_deleted", 0);
        } else if (userType == 2) {
            recordQueryWrapper.eq("buyer_id", userId);
            recordQueryWrapper.eq("buyer_deleted", 0);
        }
        recordQueryWrapper.eq("type", itemType);
        IPage<Record> recordIPage = recordMapper.selectPage(iPage, recordQueryWrapper);
        for (Record record : recordIPage.getRecords()) {
            // 添加卖家信息
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.eq("id", record.getSellerId());
            User user = userMapper.selectOne(userQueryWrapper);
            record.setSeller(user);

            if (itemType == 1) {
                // 添加商品信息
                QueryWrapper<Good> goodQueryWrapper = new QueryWrapper<>();
                goodQueryWrapper.eq("id", record.getItemId());
                Good good = goodMapper.selectOne(goodQueryWrapper);
                record.setGood(good);
            } else if (itemType == 2) {
                // 添加房屋信息
                QueryWrapper<Room> roomQueryWrapper = new QueryWrapper<>();
                roomQueryWrapper.eq("id", record.getItemId());
                Room room = roomMapper.selectOne(roomQueryWrapper);
                record.setRoom(room);
            }
        }
        return new RequestResult(HttpStatus.OK.value(), recordIPage);
    }

    @Override
    public RequestResult deleteRecord(Long id, Integer userType) {
        UpdateWrapper<Record> recordUpdateWrapper = new UpdateWrapper<>();
        recordUpdateWrapper.eq("id", id);
        if (userType == 1) {
            recordUpdateWrapper.set("seller_deleted", 1);
        } else if (userType == 2) {
            recordUpdateWrapper.set("buyer_deleted", 1);
        }
        int update = recordMapper.update(null, recordUpdateWrapper);
        if (update == 1) {
            return new RequestResult(HttpStatus.OK.value(), "删除成功！");
        } else {
            return new RequestResult(HttpStatus.INTERNAL_SERVER_ERROR.value(), "服务器错误，请联系工作人员！");
        }
    }

    @Override
    public RequestResult confirmReceipt(Long id) {
        UpdateWrapper<Record> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.eq("status", 1);
        updateWrapper.set("status", 2);
        int update = recordMapper.update(null, updateWrapper);
        if (update == 1) {
            // 获取订单对象
            QueryWrapper<Record> recordQueryWrapper = new QueryWrapper<>();
            recordQueryWrapper.eq("id", id);
            recordQueryWrapper.eq("status", 2);
            Record record = recordMapper.selectOne(recordQueryWrapper);
            // 获取卖家对象
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.eq("id", record.getSellerId());
            userQueryWrapper.eq("status", 2);
            User seller = userMapper.selectOne(userQueryWrapper);

            // 更改卖家余额
            Long result = Long.valueOf(seller.getWallet()) + Long.valueOf(record.getPrice());
            seller.setWallet(result.toString());
            userMapper.updateById(seller);
            return new RequestResult(HttpStatus.OK.value(), "已收货！");
        } else {
            return new RequestResult(HttpStatus.OK.value(), "收货失败！");
        }
    }
}
